#include "NTI_MainFrame.h"
#include <QMdiSubWindow>
#include "CwgVTKModel.h"

NTI_MainFrame::NTI_MainFrame(QWidget *parent)
    : QMainWindow(parent)
{
    //ui.setupUi(this);
	ui = new Ui::NTI_MainFrame();
	ui->setupUi(this);

	connect(ui->actionNew, &QAction::triggered, this, &NTI_MainFrame::on_NewFile);
	connect(ui->actionVTK_Model, &QAction::triggered, this, &NTI_MainFrame::on_SetupModel);
	
}

NTI_MainFrame::~NTI_MainFrame()
{
	delete ui;
}

void NTI_MainFrame::on_NewFile()
{
	QMdiSubWindow* newwin = new QMdiSubWindow(this);
	ui->mdiArea->addSubWindow(newwin);// , Qt::SubWindow);

	newwin->show();
	
	//ui->mdiArea->cascadeSubWindows();// ->showMaximized();
	QSize sizeMDI = ui->mdiArea->size();
	newwin->resize(sizeMDI.width()/2, sizeMDI.height()/2);


	QSize size = newwin->size();

	//QString sz = QString("MDI size=(%1,%2)").arg(sizeMDI.width()).arg(sizeMDI.height());
	//SendOutputMsg(sz);
	//sz = QString("SubWin size=(%1,%2)").arg(size.width()).arg(size.height());
	//SendOutputMsg(sz);

	//newwin->resize(size.width() / 2, size.height() / 2);
	//size = newwin->size();
	//sz = QString("SubWin Half size=(%1,%2)").arg(size.width()).arg(size.height());
	//SendOutputMsg(sz);
}

void NTI_MainFrame::showEvent(QShowEvent * event)
{
	return;
}

void NTI_MainFrame::resizeEvent(QResizeEvent * event)
{
	QMainWindow::resizeEvent(event);
	//on_NewFile
}


void NTI_MainFrame::on_SetupModel()
{
	QMap<int, QVector3D> mapVertex;

	const int N = 6;
	double pts[6][3] = { { 0,0,0 },{ 0,1,0 },{ 0,1,2 },{ 0,0,2 },{ 1,0,2 },{ 1,1,2 } };
	double disp[6][3] = { { 0,0,0 },{ 0,0,0 },{ 0.1,0,0 },{ 0.1,0,0 },{ 0.1,0,0.1 },{ 0.1,0,0.1 } };
	for (int i = 0; i < N; i++)
	{
		mapVertex[i + 1] = QVector3D(pts[i][0], pts[i][1], pts[i][2]);
	}

	QMap<int, QVector<int>> m_mapCellLines;
	m_mapCellLines[1] = { 1,2 };
	m_mapCellLines[2] = { 2,3 };
	m_mapCellLines[3] = { 3,4 };
	m_mapCellLines[4] = { 4,1 };
	m_mapCellLines[5] = { 4,5 };
	m_mapCellLines[6] = { 3,6 };
	m_mapCellLines[7] = { 5,6 };

	QMap<int, QVector<int>> m_mapCellSurfaces;
	m_mapCellSurfaces[1] = { 5,6,3,4 };
	m_mapCellSurfaces[2] = { 1,3,4 };
	m_mapCellSurfaces[3] = { 1,2,3 };
	//m_mapCellSurfaces = m_mapPlanes;

	QMap<int, QVector<int>> m_mapCellSolids;
	m_mapCellSolids[1] = { 1,3,4,5 };
	m_mapCellSolids[2] = { 3,4,5,6,2 };



	QMdiSubWindow* newwin = new QMdiSubWindow(this);
	CwgVTKModel *subwidget = new CwgVTKModel(newwin);

	newwin->setWidget(subwidget);
	ui->mdiArea->addSubWindow(newwin);

	newwin->show();
	newwin->resize(800, 600);

	subwidget->Set3DAxesOn();
	subwidget->SetPointsXYZ(mapVertex);
	subwidget->PlotCellsOn(en_CellType::Point, true);
	//subwidget->PlotCellsOn(en_CellType::Point, false);

	subwidget->SetCells(en_CellType::Line, m_mapCellLines);
	subwidget->PlotCellsOn(en_CellType::Line, true);

	subwidget->SetCells(en_CellType::Surface, m_mapCellSurfaces);
	subwidget->PlotCellsOn(en_CellType::Surface, true);

	subwidget->PlotCellsLabelOn(en_CellType::Point, true);
	subwidget->PlotCellsLabelOn(en_CellType::Line, true);
}


